package com.example.rabbitextensionsdemo.listener;

import com.example.rabbitextensionsdemo.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
public class QosListener {
    // 限流
//    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void qosListener(Message message) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[%s]接收到消息: %s\n", Constants.QOS_QUEUE, new String(message.getBody()));
//        channel.basicAck(deliveryTag,true);
    }
    // 非公平分发
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void qosListener1(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[%s]1接收到消息: %s, 时间: %tc\n", Constants.QOS_QUEUE, new String(message.getBody()), new Date());
        Thread.sleep(2000);
        channel.basicAck(deliveryTag,true);
    }

    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void qosListener2(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("[%s]2接收到消息: %s, 时间: %tc\n", Constants.QOS_QUEUE, new String(message.getBody()), new Date());
        Thread.sleep(3000);
        channel.basicAck(deliveryTag,true);
    }

}